package org.jerrymouse.jsa4j.db.kv;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 解析XML配置文件 * <code><?xml version="1.0" encoding="UTF-8" ?>
<jsa4j-db-kv xmlns="http://jerrymouse.org/ns/jsa4j-db-kv"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jerrymouse.org/ns/jsa4j_db_kv_1_0.xsd"
	version="1.0">
	<jsa4j-db-kv-unit name="derby-db">
		<provider>org.jerrymouse.jsa4j.db.kv.local.derby.DerbyDBManager</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class, hbm" />
			<property name="hibernate.show_sql" value="false" />
		</properties>
	</jsa4j-db-kv-unit>
</jsa4j-db-kv>
</code>
 * 
 * @author yankai
 * 
 */
class XMLConfig {
	private static Log log = LogFactory.getLog(XMLConfig.class);

	private Document document;
	private static String classpath = "classpath:";
	private static String defaultconfigPath = "classpath:META-INF/jsa4j-db-kv-default.xml";

	protected static InputStream getConfigInputStream(String path) {
		InputStream inputStream = null;
		if (path.startsWith(classpath)) {
			path = path.substring(classpath.length());
			inputStream = XMLConfig.class.getClassLoader().getResourceAsStream(
					path);
		} else {
			try {
				inputStream = new FileInputStream(new File(path));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		if (inputStream == null) {
			inputStream = getConfigInputStream(defaultconfigPath);
		}
		return inputStream;

	}

	XMLConfig(InputStream in) {
		try {
			try {
				if (in == null)
					return;
				SAXReader reader = new SAXReader();
				document = reader.read(in);
			} finally {
				if (in != null)
					in.close();
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public XMLConfig(String path) {
		this(XMLConfig.getConfigInputStream(path));
	}

	String getProvider(String name) {
		if (document == null)
			return null;
		Element unit = getUnit(name);
		Element provider = unit.element("provider");
		if (provider.getText() != null)
			return provider.getText().trim();
		return name;
	}

	private Element getUnit(String name) {
		if (document == null)
			return null;
		List<Element> elements = document.getRootElement().elements();
		for (Element element : elements) {
			Attribute attribute = element.attribute("name");
			if (attribute.getText() != null && attribute.getText().equals(name)) {
				return element;
			}
			log.trace(attribute.getText());
		}
		return null;
	}

	Map<String, String> getProperties(String name) {
		if (document == null)
			return null;
		Element unit = getUnit(name);
		Element properties = unit.element("properties");
		if (properties == null)
			return null;
		List<Element> elements = properties.elements();
		Map<String, String> map = new HashMap<String, String>();
		for (Element element : elements) {
			String k = element.attribute("name").getText();
			String v = element.attribute("value").getText();
			map.put(k, v);
		}
		return map;
	}

}